/**
 * 条件为含7或7的倍数
 * 问M之后第N个符合条件的数是多少
 * 
 * 显然是二分加数位DP
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;

vector<int> G;
llt D[22][2][7];
// has记录到目前为止有没有7
// remainder记录到目前为止7的余数
llt dfs(int pos, int has, int remainder, bool lead, bool limit){
    if(-1 == pos) {
        return 1 == has or 0 == remainder ? 1 : 0;
    }
    if(not lead and not limit and -1 != D[pos][has][remainder]) {
        return D[pos][has][remainder];
    }
    int last = limit ? G[pos] : 9;
    llt ans = 0;
    for(int i=0;i<=last;++i){
        ans += dfs(pos - 1, has | (7==i?1:0), (remainder*10+i)%7, lead&&0==i, limit&&last==i);
    }
    if(not lead and not limit){
        D[pos][has][remainder] = ans;
    }    
    return ans;
}

llt digitDP(llt n){
    G.clear();
    while(n){
        G.emplace_back(n % 10);
        n /= 10;
    }
    return dfs(G.size()-1, 0, 0, true, true);
}

llt N;
llt M;  
void work(){
    cin >> M >> N;

    auto target = digitDP(M);
    target += N;

    llt left = M, right = 1E17, mid;
    do{
        mid = (left + right) >> 1;
        auto tmp = digitDP(mid);
        if(tmp < target) left = mid + 1;
        else right = mid - 1;
    }while(left <= right);

    cout << left << endl;
    return;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);	
    // 要初始化!!!
    memset(D, -1, sizeof(D));
    int nofkase = 1;
    while(nofkase--) work();
	return 0;
}